import sys, os.path

from image import Image
from symbide.src.utils import createUniquePath
import gtk, gtk.gdk, gobject
from symbide.src.extensions.mathml.pymathml import minidom_parser
from symbide.src.extensions.mathml.pymathml.gdk_pangocairo import PangoCairoPlotter
from symbide.src.extensions.mathml.pymathml.plotter import Plotter

class MathML(Image):
    def __init__(self, DOM):
        Image.__init__(self)
        
        self.DOM = DOM
        #self.path = createUniquePath('math')
        
        pixmap = gtk.gdk.Pixmap(None, 1, 1, 24)
        pixmap.set_colormap(gtk.gdk.colormap_get_system())
        cairoContext = pixmap.cairo_create()
        self.plotter = PangoCairoPlotter(cairoContext, 1)
        self.setdom(DOM)
        
        self.draw_image()

    def draw_image(self):
        self.mathml_tree.update()    
        self.plotter.width = self.mathml_tree.width+2
        self.plotter.height = self.mathml_tree.height+2
        self.pixmap = gtk.gdk.Pixmap(None, int(self.mathml_tree.width+2), int(self.mathml_tree.height+2), 24)
        self.pixmap.set_colormap(gtk.gdk.colormap_get_system())
        self.cairoContext = self.pixmap.cairo_create()
        self.plotter.set_cairo_context(self.cairoContext)
        gobject.idle_add(self._render)
        self.image.set_from_pixmap(self.pixmap, None)
        #self.saveAs(self.path)
    
    def setdom(self, DOM):
        assert DOM.tagName == 'math'
        self.mathml_tree = minidom_parser.parseTree(DOM, self.plotter)
        self.mathml_tree.x0 = 1
        self.mathml_tree.y0 = 1
        self.mathml_tree.setAttribute("fontsize", "15pt")

    def _render(self):
        self.cairoContext.set_source_rgb(1.0,1.0,1.0)
        self.cairoContext.rectangle(-1,-1,self.plotter.width+1,self.plotter.height+1)
        self.cairoContext.fill()
        self.cairoContext.set_source_rgb(0.0,0.0,0.0)
        self.mathml_tree.update()  
        self.mathml_tree.render()
        self.queue_draw()
        
        
if __name__=='__main__':
    test = """
    <math>
    <mrow>
    <mfrac>
      <mn>1</mn>
      <mrow><mi>&#x03c3;</mi>
        <msqrt><mn>2</mn><mi>&#x03c0;</mi></msqrt>
      </mrow>
    </mfrac>
    <mo>&#x2062;</mo>
    </mrow>
    </math>
    """
    
    test2 = """ 
    <math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:mml="http://www.w3.org/1998/Math/MathML">
    <mrow>
      <mn>4</mn>
      <mo> - </mo>
      <msup>
        <mn>&#x2147;</mn>
        <mn>15</mn>
      </msup>
      <mo>&#x2062;</mo>
      <mrow>
        <mo>sin</mo>
        <mspace width="0.01em" linebreak="nobreak"/>
        <msup>
          <mn>&#x2147;</mn>
          <mi>x</mi>
        </msup>
      </mrow>
      <mo> + </mo>
      <mn>2</mn>
      <mo>&#x2062;</mo>
      <mfrac>
        <mrow>
          <mi>b</mi>
        </mrow>
        <mrow>
          <mi>c</mi>
        </mrow>
      </mfrac>
    </mrow>
</math>
"""
    from xml.dom import minidom
    dom = minidom.parseString(test2)
    image = MathML(dom.documentElement)
    
    text = gtk.TextView()
    text.show()
    
    
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.add(image)
    window.connect("destroy", gtk.main_quit)
    window.show_all()
    gtk.main()

